package main.java.com.itlin.datastructure.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

public class SinglyLinkedList implements Iterable<Integer> {//整体

    //head 指针
    private Node head = null;

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node pointer = head;
            @Override
            public boolean hasNext() {//询问是否有下一个元素
                return pointer != null;
            }

            @Override
            public Integer next() {//返回当前元素的值，并且指向下一个元素
                int value = pointer.value;
                pointer = pointer.next;
                return value;
            }
        };
    }

    //节点类
    private static class Node {
        int value; //值
        Node next;//下一个节点指针

        public Node(int value ,Node next){
            this.value = value;
            this.next = next;
        }
    }

    public void addFirst(int value) {
        //1.链表为空的情况
        //1.1 先创建节点对象
        //Node node = new Node(value, null);
        //1.2 将头指针指向新节点
        //head  = node;
        //2.链表非空的情况
        //1.1 先创建节点对象
        head = new Node(value, head);
        //1.2 将头指针指向新节点
    }

    public void loop1(Consumer<Integer> consumer){
        Node pointer =  head;
        while(pointer != null){
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }

    public void loop2(Consumer<Integer> consumer){
        for (Node pointer = head; pointer != null; pointer = pointer.next){
            consumer.accept(pointer.value);
        }
    }

    public void loop3(Consumer<Integer> before,Consumer<Integer> after){
        recursion(head,before,after);
    }

    private void recursion(Node curr, Consumer<Integer> before, Consumer<Integer> after){//针对某个节点进行的操作
        if (curr == null) return;

        before.accept(curr.value);
        recursion(curr.next, before, after);
        after.accept(curr.value);

    }

    private Node findLast(){
        //1.链表为空的情况
        if (head == null){
            return null;
        }
        //2.链表非空的情况
        Node last = head;
        while(last.next != null){
            last = last.next;
        }
        return last;
    }

    public void addLast(int value){
        Node last = findLast();
        if (last == null){
            addFirst(value);
            return ;
        }
        last.next = new Node(value, null);
    }

    private Node findNode(int index){
        int i = 0;
        for (Node pointer = head; pointer != null ; pointer = pointer.next, i++){
            if (i == index){
                return pointer;
            }
        }
        return null;
    }

    public int get(int index) throws IllegalArgumentException{
        Node node = findNode(index);
        if(node == null){
            throw illegalIndex(index);
        }else {
            return node.value;
        }
    }

    private static IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index[%d] 不合法%n", index));
    }

    public void insert(int index,int value){
        if (index == 0){
            addFirst(value);
            return ;
        }

        //非法情况
        Node previous = findNode(index - 1);//找到上一个节点

        if (previous == null){
            throw illegalIndex(index);
        }
        previous.next  = new Node(value, previous.next);

    }

    public void removeFirst(){
        if (head == null) {
            throw illegalIndex(0);
        }
        head = head.next;
    }

    public void remove(int index){

        if (index == 0){
            removeFirst();
            return;
        }
        Node previous = findNode(index - 1);

        if (previous == null){
            throw illegalIndex(index);
        }
        Node removed = previous.next;
        if (removed == null){
            throw illegalIndex(index);
        }
        previous.next = removed.next;
    }



}


